package cn.wangdian.erp.demo;

import cn.wangdian.erp.sdk.Client;
import cn.wangdian.erp.sdk.Pager;
import cn.wangdian.erp.sdk.WdtErpException;
import cn.wangdian.erp.sdk.api.purchaseapply.PurchaseApplyAPI;
import cn.wangdian.erp.sdk.api.purchaseapply.dto.PurchaseApplySearchRequest;
import cn.wangdian.erp.sdk.api.purchaseapply.dto.PurchaseApplySearchResponse;
import cn.wangdian.erp.sdk.api.purchaseapply.dto.PurchaseApplyUploadRequest;
import cn.wangdian.erp.sdk.api.purchaseapply.dto.PurchaseApplyUploadResponse;
import cn.wangdian.erp.sdk.impl.ApiFactory;
import cn.wangdian.erp.sdk.impl.DefaultClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PurchaseApply
{
	public static void main(String[] args) throws WdtErpException
	{
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
		PurchaseApplyAPI api = ApiFactory.get(client, PurchaseApplyAPI.class);

//		testSearch(api);
		testUpload(api);
	}

	private static void testSearch(PurchaseApplyAPI api) throws WdtErpException
	{
		int pageSize = 1;
		Pager pager = new Pager(pageSize, 0, true);

		PurchaseApplySearchRequest request = new PurchaseApplySearchRequest();
		request.setStartTime("2022-03-01");
		request.setEndTime("2022-03-31");

		PurchaseApplySearchResponse response = api.search(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.search(request, pager);
			System.out.println("处理数据");
		}
	}

	private static void testUpload(PurchaseApplyAPI api) throws WdtErpException
	{
		PurchaseApplyUploadRequest request = new PurchaseApplyUploadRequest();
		PurchaseApplyUploadRequest.Detail detail = new PurchaseApplyUploadRequest.Detail();
		List<PurchaseApplyUploadRequest.Detail> detailList = new ArrayList<>();
		detailList.add(detail);
		request.setDetailList(detailList);

		detail.setSpecNo("gy01");
		detail.setNum(new BigDecimal("2"));
		detail.setRemark("api upload test");

		PurchaseApplyUploadResponse response = api.upload(request);
		System.out.println("采购申请单号:"+response.getMessage());
	}
}
